package com.xsm.activitygroup;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;


import com.xsm.DataBase.SaveMessage;
import com.xsm.adapter.SelectAdapter;
import com.xsm.internet.PostMessage;
import com.xsm.view.LoadDialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ChangeChose extends Activity{
ListView schooListView;
boolean[] isselect;
int index=0;
LoadDialog dialog;
Context context;
String user;
ArrayList<String> valueStrings;
EDHandler handler;
String type="";
protected void onCreate(Bundle savedInstanceState) {
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.changeschool);
	schooListView=(ListView)findViewById(R.id.schoolList);
	String value=getIntent().getExtras().getString("value");
	SharedPreferences sharedPreferences=getSharedPreferences("XSM", 0);
	user=sharedPreferences.getString("User", "");
	type=getIntent().getExtras().getString("type");
    valueStrings=new ArrayList<String>();
	context=this;
	handler=new EDHandler();
	if(type.equals("school")){
	valueStrings.add("广州中医药大学");
	valueStrings.add("广东药学院");
	valueStrings.add("华南理工大学");
	valueStrings.add("广东工业大学");
	valueStrings.add("广州大学");
	valueStrings.add("广州美术学院");
	valueStrings.add("广东外语外贸大学");
	valueStrings.add("星海音乐学院");
	valueStrings.add("中山大学");
	valueStrings.add("华南师范大学");}
	else {
		valueStrings.add("男");	
		valueStrings.add("女");	
	}
for(int i=0;i<valueStrings.size();i++){
	if(valueStrings.get(i).equals(value))
		index=i;
}
	final SelectAdapter adapter=new SelectAdapter(this, valueStrings);
	adapter.setselect(index, true);
	adapter.notifyDataSetChanged();
	schooListView.setAdapter(adapter);
	schooListView.setOnItemClickListener(new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			isselect=adapter.getselect();
		for(int i=0;i<isselect.length;i++){
			if(i==position){
			adapter.setselect(i,true);	
			index=i;
			}
			else
			adapter.setselect(i,false);	
		}
		adapter.notifyDataSetChanged();
		}
	});
}
public void Back(View view){
	finish();
}
public void SaveSchool(View view){
	dialog=new LoadDialog(context,"正在提交...");
	new Thread(new Runnable() {
		public void run() {
			List params=new ArrayList();
			params.add(new BasicNameValuePair("ASK","UpdateUserMessage"));
			params.add(new BasicNameValuePair("user",user));
			params.add(new BasicNameValuePair("value",valueStrings.get(index)));
			params.add(new BasicNameValuePair("type",type));
			PostMessage postMessage=new PostMessage(params);
	       String 	result=postMessage.getResult();
			Message msgMessage=handler.obtainMessage();
			if(result.equals("-1")){
				msgMessage.what=-1;
			}else if(result.equals("1")){
				msgMessage.what=1;
			new SaveMessage(context, user).UpdateMessage(user,type,valueStrings.get(index));	
			}
			handler.sendMessage(msgMessage);
		}
	}).start();
}

class EDHandler extends Handler{
public void handleMessage(Message msg) {
if(msg.what==-1){
	Toast.makeText(context,"连接服务器失败", Toast.LENGTH_LONG).show();
}else {
	Toast.makeText(context,"更新成功", Toast.LENGTH_LONG).show();	
	Intent intent=new Intent();
	intent.putExtra("result",valueStrings.get(index));
	setResult(RESULT_OK,intent);
	finish();
}
}


}

}
